package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Flight;
import cn.wolfcode.luowowo.article.domain.FlightOrder;
import cn.wolfcode.luowowo.article.mapper.FlightOrderMapper;
import cn.wolfcode.luowowo.article.service.IFlightOrderService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class FlightOrderServiceImpl implements IFlightOrderService {

    private static String JPDD = "JPDD";

    @Autowired
    private FlightOrderMapper flightOrderMapper;

   /* @Reference
    private IUserGoldService userGoldService;


    @Reference
    private IUserPaymentsService userPaymentsService;*/

    @Override
    public AjaxResult saveOrder(Long fid, String depTime, Long uid) {

        FlightOrder flightOrder = new FlightOrder();
        Flight flight = new Flight();
        flight.setId(fid);
        flightOrder.setFlightId(flight);


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //出行日期
        Date parse = null;
        try {
            parse = sdf.parse(depTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        flightOrder.setDateTime(parse);
        //下单用户
        UserInfo userInfo = new UserInfo();
        userInfo.setId(uid);
        flightOrder.setUserId(userInfo);

        //订单号
        Date date = new Date();
        String str = "yyyMMddHHmmss";
        SimpleDateFormat sdf1 = new SimpleDateFormat(str);
        String format = sdf1.format(date);
        Random random = new Random();
        int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;
        flightOrder.setFOrder(JPDD + format + rannum);

        //下单时间
        flightOrder.setOrderTime(new Date());

        flightOrder.setStatus(1);

        flightOrderMapper.save(flightOrder);

       /* //增加积分
        userGoldService.add(1500, uid);
        //下单记录
        UserPayments userPayments = new UserPayments();
        userPayments.setCreateDate(new Date());
        userPayments.setOrder(JPDD + format + rannum);
        userPayments.setTheme("机票订购");
        userPayments.setInfo("飞到很远地方的机票");
        userPayments.setVal("+" + 1500);
        userPayments.setUserId(uid);
        userPaymentsService.inser(userPayments);*/


        return new AjaxResult();
    }

    @Override
    public List<FlightOrder> selectOrderByUid(Long uid) {


        return flightOrderMapper.finlOrderByUid(uid);
    }

    @Override
    public List<FlightOrder> selectOrderByOld(Long id) {

        return flightOrderMapper.finlOrderByOld(id);
    }

    @Override
    public List<FlightOrder> selectOrderByQu(Long id) {
        return flightOrderMapper.finlOrderByQu(id);
    }


}
